﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.TeamFoundation.Client;

namespace TFSCommon.TFS
{
    public partial class TfsServer : Form
    {
        public TfsServer()
        {
            InitializeComponent();
        }

        private void TFSServer_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            System.Diagnostics.Process.Start("http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(VS.TFC.ADDREMOVETFSDIALOG);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV3.5%22);k(DevLang-CSHARP)&rd=true");
        }

        private void TFSServer_Load(object sender, EventArgs e)
        {
            RefreshServerList();
        }

        private void gridServerList_SelectionChanged(object sender, EventArgs e)
        {
            btnRemove.Enabled = true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            TfsAddServer frm = new TfsAddServer();

            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                RefreshServerList();
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            btnRemove.Enabled = false;
            RegisteredTfsConnections.UnregisterConfigurationServer(gridServerList.SelectedRows[0].Cells[0].Value.ToString());

            RefreshServerList();
        }

        private void RefreshServerList()
        {
            btnRemove.Enabled = false;
            dsServerList.dtServerList.Clear();

            RegisteredConfigurationServer[] servers = RegisteredTfsConnections.GetConfigurationServers();

            foreach (RegisteredConfigurationServer server in servers)
            {
                dsServerList.dtServerList.AdddtServerListRow(server.Name, server.Uri.AbsoluteUri);
            }
        }
    }
}
